//
//  TYZAreaPickerView.m
//  DriveSchoolCoach
//
//  Created by TangBin on 2019/8/7.
//  Copyright © 2019 TangBin. All rights reserved.
//

#import "TYZAreaPickerView.h"
#import "EDSCustomLeftRightBtnView.h"
#import "UIWindow+AreaPickerView.h"

@implementation TYZAreaModel
@end

@interface TYZAreaPickerView () <UIPickerViewDelegate, UIPickerViewDataSource>

@property (nonatomic, strong) TYZAreaModel *locate;

@property (nonatomic, strong) UIPickerView *locatePicker;
@property (nonatomic, assign) TYZAreaPickerStyle pickerStyle;

@property (nonatomic, strong) NSArray *provinces;

@property (nonatomic, strong) NSArray *cities;

@property (nonatomic, strong) NSArray *areas;

@property (nonatomic, assign) BOOL autoSelected;
@property (nonatomic, strong) EDSCustomLeftRightBtnView *topView;

- (void)initWithVar;

- (void)initWithSubView;

- (void)initWithTopView;

@end

@implementation TYZAreaPickerView

- (instancetype)initWithFrame:(CGRect)frame pickerStyle:(TYZAreaPickerStyle)pickerStyle autoSelected:(BOOL)autoSelected
{
    if (self = [super initWithFrame:frame])
    {
        self.pickerStyle = pickerStyle;
        self.autoSelected = autoSelected;
        
        [self initWithVar];
        
        [self initWithSubView];
    }
    
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    return [self initWithFrame:frame pickerStyle:TYZAreaPickerWithStateAndCity autoSelected:YES];
}

- (void)initWithVar
{
    
    _locate = [[TYZAreaModel alloc] init];
    
    //加载数据
    if (_pickerStyle == TYZAreaPickerWithStateAndCityAndDistrict)
    {
        _provinces = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"area.plist" ofType:nil]];
        self.cities = [[_provinces objectAtIndex:0] objectForKey:@"cities"];
        self.locate.state = [[_provinces objectAtIndex:0] objectForKey:@"state"];
        self.locate.city = [[_cities objectAtIndex:0] objectForKey:@"city"];
        
        self.areas = [[_cities objectAtIndex:0] objectForKey:@"areas"];
        if ([_areas count] > 0)
        {
            self.locate.district = [_areas objectAtIndex:0];
        }
        else
        {
            self.locate.district = @"";
        }
        
    }
    else
    {
        _provinces = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"city.plist" ofType:nil]];
        self.cities = [[_provinces objectAtIndex:0] objectForKey:@"cities"];
        self.locate.state = [[_provinces objectAtIndex:0] objectForKey:@"state"];
        self.locate.city = [_cities objectAtIndex:0];
    }
}

- (void)initWithSubView
{
    self.backgroundColor = [UIColor whiteColor];
    [self initWithTopView];
    
    [self initWithLocatePicker];
}

- (void)initWithTopView
{
    CGRect frame = CGRectMake(0, 0, kScreenWidth, kEDSCustomLeftRightBtnViewHeight);
    _topView = [[EDSCustomLeftRightBtnView alloc] initWithFrame:frame leftTitle:@"取消" leftColor:[UIColor blackColor] rightTitle:@"确认" rightColor:[UIColor colorWithHexString:@"#3399FF"]];
    [self addSubview:_topView];
    
    @weakify(self);
    _topView.touchButtonBlock = ^(EN_BUTTON_TYPE_TAG tag) {
        @strongify(self);
        if (tag == EN_BUTTON_TYPE_SUBMIT_TAG)
        {// 确认
            if (self.touchDateBlock)
            {
                self.touchDateBlock(self.locate);
            }
            [self hide];
        }
        else
        {
            [self hide];
        }
    };
}

- (void)initWithLocatePicker
{
    _locatePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, kScreenWidth, 216)];
    _locatePicker.delegate = self;
    _locatePicker.dataSource = self;
    _locatePicker.showsSelectionIndicator = YES;
    [_locatePicker setAutoresizesSubviews:YES];
    [_locatePicker setUserInteractionEnabled:YES];
    [_locatePicker setContentMode:UIViewContentModeScaleToFill];
    [self addSubview:_locatePicker];
}

- (void)show
{
    if (!kCSKeyWindow.areaPicker)
    {
        // 创建蒙版
        UIView *maskView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        maskView.backgroundColor = [UIColor blackColor];
        maskView.alpha = 0;
        [kCSKeyWindow addSubview:maskView];
        kCSKeyWindow.areaPickerMaskView = maskView;
        
        [kCSKeyWindow addSubview:self];
        kCSKeyWindow.areaPicker = self;
        
        //        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
        //        [maskView addGestureRecognizer:tap];
        CGRect frame = self.frame;
        frame.origin.y = kScreenHeight - kTYZAreaPickerViewHeight;
        [UIView animateWithDuration:0.23 animations:^{
            maskView.alpha = 0.4;
            //            self.alpha = 1;
            self.frame = frame;
        }];
    }
}

- (void)hide
{
    if (kCSKeyWindow.areaPicker)
    {
        @weakify(self);
        CGRect frame = self.frame;
        frame.origin.y = kScreenHeight;
        [UIView animateWithDuration:0.2 animations:^{
            @strongify(self);
            //            self.alpha = 0;
            self.frame = frame;
            kCSKeyWindow.areaPickerMaskView.alpha = 0;
        } completion:^(BOOL finished) {
            [kCSKeyWindow.areaPicker removeFromSuperview];
            [kCSKeyWindow.areaPickerMaskView removeFromSuperview];
            kCSKeyWindow.areaPickerMaskView = nil;
            kCSKeyWindow.areaPicker = nil;
        }];
    }
}

#pragma mark - UIPickerViewDelegate, UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    if (_pickerStyle == TYZAreaPickerWithStateAndCityAndDistrict)
    {
        return 3;
    }
    else
    {
        return 2;
    }
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    switch (component)
    {
        case 0:
            return [_provinces count];
            break;
        case 1:
            return [_cities count];
            break;
        case 2:
            if (self.pickerStyle == TYZAreaPickerWithStateAndCityAndDistrict)
            {
                return [_areas count];
                break;
            }
        default:
            return 0;
            break;
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (_pickerStyle == TYZAreaPickerWithStateAndCityAndDistrict)
    {
        switch (component)
        {
            case 0:
                return [[_provinces objectAtIndex:row] objectForKey:@"state"];
                break;
            case 1:
                return [[_cities objectAtIndex:row] objectForKey:@"city"];
                break;
            case 2:
                if ([_areas count] > 0)
                {
                    return [_areas objectAtIndex:row];
                    break;
                }
            default:
                return  @"";
                break;
        }
    }
    else
    {
        switch (component)
        {
            case 0:
                return [[_provinces objectAtIndex:row] objectForKey:@"state"];
                break;
            case 1:
                return [_cities objectAtIndex:row];
                break;
            default:
                return @"";
                break;
        }
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (_pickerStyle == TYZAreaPickerWithStateAndCityAndDistrict)
    {
        switch (component)
        {
            case 0:
                self.cities = [[_provinces objectAtIndex:row] objectForKey:@"cities"];
                [self.locatePicker selectRow:0 inComponent:1 animated:YES];
                [self.locatePicker reloadComponent:1];
                
                self.areas = [[_cities objectAtIndex:0] objectForKey:@"areas"];
                [self.locatePicker selectRow:0 inComponent:2 animated:YES];
                [self.locatePicker reloadComponent:2];
                
                self.locate.state = [[_provinces objectAtIndex:row] objectForKey:@"state"];
                self.locate.city = [[_cities objectAtIndex:0] objectForKey:@"city"];
                if ([_areas count] > 0)
                {
                    self.locate.district = [_areas objectAtIndex:0];
                }
                else
                {
                    self.locate.district = @"";
                }
                break;
            case 1:
                self.areas = [[_cities objectAtIndex:row] objectForKey:@"areas"];
                [self.locatePicker selectRow:0 inComponent:2 animated:YES];
                [self.locatePicker reloadComponent:2];
                
                self.locate.city = [[_cities objectAtIndex:row] objectForKey:@"city"];
                if ([_areas count] > 0)
                {
                    self.locate.district = [_areas objectAtIndex:0];
                }
                else
                {
                    self.locate.district = @"";
                }
                break;
            case 2:
                if ([_areas count] > 0)
                {
                    self.locate.district = [_areas objectAtIndex:row];
                }
                else
                {
                    self.locate.district = @"";
                }
                break;
            default:
                break;
        }
    }
    else
    {
        switch (component)
        {
            case 0:
                self.cities = [[_provinces objectAtIndex:row] objectForKey:@"cities"];
                [self.locatePicker selectRow:0 inComponent:1 animated:YES];
                [self.locatePicker reloadComponent:1];
                
                self.locate.state = [[_provinces objectAtIndex:row] objectForKey:@"state"];
                self.locate.city = [_cities objectAtIndex:0];
                break;
            case 1:
                self.locate.city = [_cities objectAtIndex:row];
                break;
            default:
                break;
        }
    }
    if (self.touchDateBlock)
    {
        if (self.autoSelected)
        {
            self.touchDateBlock(_locate);
        }
    }
}


@end
